package gov.pbc.nn.kjgl.jfcr.dataInterceptor;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Component;

import sun.misc.BASE64Decoder;

import com.bstek.bdf2.export.interceptor.IDataInterceptor;

@Component
public class myInterceptor implements IDataInterceptor {

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "myinterceptor";
	}

	@Override
	public String getDesc() {
		// TODO Auto-generated method stub
		return "描述";
	}

	@Override
	public void interceptGridData(List<Map<String, Object>> list) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void interceptAutoFormData(List<Map<String, Object>> list) throws Exception {
		for (Map<String, Object> user : list) {
			System.out.println(user);
			if (user.get("label").equals("signIn")) {
				String string = (String) user.get("value");
				Image jpg = Toolkit.getDefaultToolkit().createImage(string.getBytes());
				user.put("value",jpg);
				break;

			}
			// if (user.get("signOut") != null) {
			//
			// user.put("enabled", ((Boolean) user.get("enabled")) == true ? "是"
			// : "否");
			// }
		}

	}

	public static byte[] GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
		byte[] b = null;
		if (imgStr != null) // 图像数据为空
		{
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				// Base64解码
				b = decoder.decodeBuffer(imgStr);
				for (int i = 0; i < b.length; ++i) {
					if (b[i] < 0) {// 调整异常数据
						b[i] += 256;
					}
				}
				// 生成jpeg图片
				// String imgFilePath = "d://222.jpg";// 新生成的图片
//				ByteArrayInputStream bais = new ByteArrayInputStream(imgStr.getBytes());
//				ImageIO.read(bais);
				 Image jpg = Toolkit.getDefaultToolkit().createImage(imgStr.getBytes());
				//
				//
				// OutputStream out = new FileOutputStream(imgFilePath);
				// out.write(b);
				// out.flush();
				// out.close();
				// return true;
			} catch (Exception e) {

			}
//			return b;
		}
		return b;
	}
}
